/**
 * Created by liubin on 2017/4/27.
 */



var DzpkCard = cc.Sprite.extend({

    mCardNum : null,
    mNumSprite : null, //数字
    mTypeSprite : null, //花色
    mTypeSmallSprite : null, //花色
    mCardSprite : null,  //牌面

    mCardlight : null, //maxcard 高亮
    mCardMask : null, //灰板


    ctor : function(){
        this._super();
        this.setAnchorPoint(cc.p(0.5, 0.5));

        this.mCardNum = 0;

        this.mCardSprite = new cc.Sprite();
        this.mNumSprite = new cc.Sprite();
        this.mTypeSprite = new cc.Sprite();
        this.mTypeSmallSprite = new cc.Sprite();
        this.mCardlight = new cc.Sprite();
        this.mCardMask = new cc.Sprite();

        this.mCardlight.setSpriteFrame("cardlight.png");
        this.mCardlight.setVisible(false);

        this.mCardMask.setSpriteFrame("cardface_mask.png");
        this.mCardMask.setVisible(false);

        this.mTypeSprite.setAnchorPoint(cc.p(1, 0));

        this.addChild(this.mCardSprite);
        this.addChild(this.mNumSprite);
        this.addChild(this.mTypeSprite);
        this.addChild(this.mTypeSmallSprite);
        this.addChild(this.mCardlight);
        this.addChild(this.mCardMask);

        this.mNumSprite.setPosition(cc.p(-25.5, 32));
        this.mTypeSprite.setPosition(cc.p(40.5, -54));
        this.mTypeSmallSprite.setPosition(cc.p(-25.5, 0));

        this.setScale(0.9);
    },

    reset : function () {
        this.stopAllActions();
        this.mCardNum = 0;
        this.mCardlight.setVisible(false);
        this.mNumSprite.setVisible(false);
        this.mTypeSprite.setVisible(false);
        this.mTypeSmallSprite.setVisible(false);
        this.mCardMask.setVisible(false);
        this.mCardSprite.setSpriteFrame("cardback.png");
    },

    setCardNum : function(num){
        if (mo.dzpkCardHelper.getExistCardNum(num)){
            this.mCardNum = num;
        }else{
            this.mCardNum = 0;
        }
    },

    setNumSprite : function () {
        if(this.mCardNum == 0){
            this.mNumSprite.setVisible(false);
            this.mTypeSprite.setVisible(false);
            this.mTypeSmallSprite.setVisible(false);
        }else{
            this.mNumSprite.setVisible(true);
            this.mTypeSprite.setVisible(true);
            this.mTypeSmallSprite.setVisible(true);

            var suit = this.getCardSuit(this.mCardNum);

            var absCardNum = mo.dzpkCardHelper.getAbsCardNum(this.mCardNum);

            this.mNumSprite.setSpriteFrame(this.getCardNumFileName() + absCardNum + ".png");
            this.mTypeSmallSprite.setSpriteFrame("card_type_s_" + (suit+1) +  ".png");

            if (absCardNum<=10){
                this.mTypeSprite.setSpriteFrame("card_type_" + (suit+1) +  ".png");
            }else{
                this.mTypeSprite.setSpriteFrame("card_type_" + absCardNum+ "_" + (suit+1) +  ".png");
            }
        }
    },

    getCardNumFileName : function () {
        var suit = this.getCardSuit(this.mCardNum);

        if(suit == 0 || suit == 2){
            return "card_";
        }else{
            return "card_r_";
        }


    },

    //kind  1:inhand 2:public 3:自己的手牌
    setCardKind : function(kind){
        if (kind == 1){
            if(this.mCardNum == 0){
                this.mCardSprite.setSpriteFrame("cardsmall_back.png");
            }else{
                this.mCardSprite.setSpriteFrame("cardface.png");
            }

        }else if(kind == 2){
            if(this.mCardNum == 0){
                this.mCardSprite.setSpriteFrame("cardback.png");
            }else{
                this.mCardSprite.setSpriteFrame("cardface.png");
            }
        }else if(kind == 3){
            if(this.mCardNum == 0){
                this.mCardSprite.setSpriteFrame("cardback.png");
            }else{
                this.mCardSprite.setSpriteFrame("cardface.png");
            }

        }
        this.setNumSprite();
    },



    //返回牌花色 0:tao 1:xin 2:mei 3:fang
    getCardSuit : function(num){
        var temp = num - 1;
        return parseInt(temp / 13);
    },


    //翻牌
    fanPai : function (kind) {
        this.stopAllActions();

        var rotatoX = this.getRotationX()

        this.runAction(cc.sequence(
            cc.rotateTo(0.2, this.getRotationX(), 90),
            cc.callFunc(function(){
                this.setCardKind(kind);
                this.setRotationX(this.getRotationX());
                this.setRotationY(-90);

                if(kind==1){
                    var pos = this.getPosition();
                    this.setPosition(cc.p(pos.x + 30, pos.y + 30));
                }

            }, this),
            cc.rotateTo(0.01, rotatoX)
        ));
    },

});